import 'dart:ui';

import 'package:flutter/material.dart';
import '../entity/user_entity.dart';
import '../util/dio_util.dart';
import '../util/my_colors.dart';
import '../util/shared_preferences_util.dart';
import '../custom_widget/my_drawer_body_item.dart';

class MyDrawer extends StatelessWidget {
  UserData _userData;
  BuildContext _context;

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _initData(context),
      builder: _buildFuture,
    );
  }

  Widget _buildFuture(BuildContext context, AsyncSnapshot snapshot) {
    switch (snapshot.connectionState) {
      case ConnectionState.waiting:
        return CircularProgressIndicator();
      case ConnectionState.done:
        return _drawer();
    }
  }

  Widget _drawer() {
    return Drawer(
      child: Column(
        children: <Widget>[
          Expanded(
            child: _drawerHead(),
            flex: 1,
          ),
          Expanded(
            child: Container(
              child: _drawerBody(),
              color: MyColors.mainColor,
            ),
            flex: 3,
          )
        ],
      ),
    );
  }

  Future _initData(BuildContext context) async {
    _context = context;
    _userData = await SpUtilForUser().getUser();
  }

  Widget _drawerHead() {
    return Row(
      children: <Widget>[
        Expanded(
          //使用DecoratedBox(装饰盒子)和BackdropFilter(背景模糊)配合进行背景模糊处理
            child: DecoratedBox(
              decoration: BoxDecoration(
                  image: DecorationImage(
                      image: NetworkImage(
                          DioUtil.head_url + _userData.headImg),
                      fit: BoxFit.cover)),
              position: DecorationPosition.background,

              //在BackdropFilter外套一层ClipRect可以限制其模糊范围为子组件的大小
              child: ClipRect(
                child: BackdropFilter(
                  child: _headItem(),
                  filter: ImageFilter.blur(sigmaY: 20, sigmaX: 20),
                ),
              ),
            ))
      ],
    );
  }

  Widget _headItem() {
    double headRadius = 70.0;
    return Stack(
      children: <Widget>[
        Padding(
          padding: EdgeInsets.fromLTRB(
              20, MediaQuery
              .of(_context)
              .padding
              .top + 20, 0, 0),
          child: Align(
            child: GestureDetector(
              child: Column(
                children: <Widget>[
                  //头像
                  Container(
                    child: ClipOval(
                      child: Image.network(
                        DioUtil.head_url + _userData.headImg,
                        fit: BoxFit.cover,
                        height: headRadius,
                        width: headRadius,
                      ),
                    ),
                    decoration: BoxDecoration(
                        border: new Border.all(width: 1, color: Colors.white),
                        borderRadius: BorderRadius.circular(headRadius)),
                  ),

                  //名字
                  Text(
                    _userData.name,
                    style: TextStyle(fontSize: 30, color: Colors.white),
                  )
                ],
              ),
              onTap: () {},
            ),
            alignment: Alignment.topLeft,
          ),
        ),
        Padding(
          padding: EdgeInsets.fromLTRB(
              0, MediaQuery
              .of(_context)
              .padding
              .top + 10, 10, 0),
          child: Align(
            alignment: Alignment.topRight,
            child: IconButton(
              icon: Icon(
                Icons.exit_to_app,
                color: Colors.white,
              ),
              onPressed: () {},
            ),
          ),
        )
      ],
    );
  }

  Widget _drawerBody() {
    BorderRadiusGeometry bottomRadius = BorderRadius.only(
        bottomLeft: Radius.circular(20),
        bottomRight: Radius.circular(20));

    return Column(
      children: <Widget>[
        Container(
          decoration: BoxDecoration(
              color: MyColors.subColor,
              borderRadius: bottomRadius),
          child: ListView(
            shrinkWrap: true,
            children: <Widget>[
              DrawerBodyItem(icon: Icons.public, title: "圈子"),
              DrawerBodyItem(icon: Icons.perm_identity, title: "关注的人"),
              DrawerBodyItem(icon: Icons.shopping_cart, title: "购物车"),
              DrawerBodyItem(icon: Icons.settings, title: "设置", borderRadius:bottomRadius,),
            ],
            padding: EdgeInsets.all(0),
          ),
        ),
      ],
    );
  }
}


